//
//  AreaViewController.m
//  HomeFurnishing
//
//  Created by dllo on 16/4/11.
//  Copyright © 2016年 YMing. All rights reserved.
//

#import "AreaViewController.h"
#import "NSString+Characters.h"
@interface AreaViewController ()<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchControllerDelegate>
@property(nonatomic, retain)NSArray *dataArr;//首字母数组
@property(nonatomic, retain)NSArray *dataHArr;//热门数组
@property(nonatomic, retain)NSMutableDictionary *dataDic;//城市数组
@property(nonatomic, retain)UITableView *tableV;
@property(nonatomic, retain)UISearchBar *seaBar;
@property(nonatomic, retain)UISearchController *searchPlay;
@property(nonatomic, retain)NSArray *searchArr;
@property (nonatomic,assign) BOOL isSearch;//是搜索状态
@end

@implementation AreaViewController

-(void)dealloc
{
    [_dataArr release];
    [_dataDic release];
    [_tableV release];
    [_dataHArr release];
    [_seaBar release];
    [_searchPlay release];
    [_searchArr release];
    Block_release(_secBlock);
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"城市列表";
    
    [self dataHandle];
    self.dataHArr = @[@"全国",@"广州",@"成都",@"上海",@"北京",@"深圳"];
    self.tableV = [[UITableView alloc] initWithFrame:self.view.frame style:(UITableViewStylePlain)];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    [self.tableV registerClass:[UITableViewCell class] forCellReuseIdentifier:@"AreaVCCell"];
    //[self createSearchBar];
}

-(void)dataHandle
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"txt"];
    NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *strArr = [str componentsSeparatedByString:@"\n"];
     NSMutableArray *proArray = [NSMutableArray array];
        for (NSString *tempStr in strArr) {
           if ([tempStr hasPrefix:@"  "] && ![tempStr hasPrefix:@"   "]){
              NSArray *arr = [tempStr componentsSeparatedByString:@" "];
               [proArray addObject:arr[2]];
            
    }
  }
   // NSLog(@"%@",proArray);
    self.dataDic = [NSMutableDictionary dictionary];
    self.dataArr = [NSArray array];
    for (NSString *temp in proArray) {
        [self addString:temp];
    }
    
    self.dataArr = [self.dataDic.allKeys sortedArrayUsingSelector:@selector(compare:)];
//    NSLog(@"%@",self.dataArr);
//    NSLog(@"%@",self.dataDic);
}
-(void)addString:(NSString *)str
{
    NSString *shouZiMu = [str firstCharacterOfName];
    NSMutableArray *tempArr = [self.dataDic objectForKey:shouZiMu];
    
    if (!tempArr) {
        tempArr = [NSMutableArray array];
        [self.dataDic setObject:tempArr forKey:shouZiMu];
    }
    [tempArr addObject:str];
}
//分区个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataDic.count+1;
}
//分区头字符串
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (0==section) {
        return @"热门城市";
    }else{
        
        return  self.dataArr[section-1];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (0==section) {
        return self.dataHArr.count;
    } else {
        
        NSString *key = self.dataArr[section-1];
        NSMutableArray *arr = self.dataDic[key];
        return arr.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AreaVCCell" forIndexPath:indexPath];
    if (0==indexPath.section) {
        cell.textLabel.text = self.dataHArr[indexPath.row];
    } else {
        
        cell.textLabel.text = self.dataDic[self.dataArr[indexPath.section-1]][indexPath.row];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (0==indexPath.section) {
        self.secBlock(self.dataHArr[indexPath.row]);
    } else {
        self.secBlock(self.dataDic[self.dataArr[indexPath.section-1]][indexPath.row]);
    }
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
